/**
 **************************************************************
 通用工具 by Being Nobody
 **************************************************************
 */

/**
 * log
 * */
/*export const log = function() {
    console.log.apply(console, arguments)
}*/

/**
 * log
 * */
export const log = console.log.bind(console)

/**
 * 测试工具
 * */
export const ensure = (condition, message) => {
    if (condition) {
        log(message)
    }
}

/**
 * 增强测试工具
 * */
export const ensureEqual = (a, b, message) => {
    if (a != b) {
        log(`测试成功，${a}不等于${b},${message}`)
    }
}

/**
 * 查询单个节点
 * */
export const e = function (selector) {
    return document.querySelector(selector)
}

/**
 * 查询多个节点
 * */
export const es = function (selector) {
    return document.querySelectorAll(selector)
}

/**
 * 为某个节点后添加html
 * */
export const appendHtml = function (element, html) {
    element.insertAdjacentHTML('beforeend', html)
}

/**
 * 绑定事件
 * */
export const bindEvent = function (element, eventName, callback) {
    element.addEventListener(eventName, callback)
}

/**
 * 切换class
 * */
export const toggleClass = function (element, className) {
    if (element.classList.contains(className)) {
        element.classList.remove(className)
    } else {
        element.classList.add(className)
    }
}

/**
 * 删除所有class
 * */
export const removeClassAll = function (className) {
    var selector = '.' + className
    var elements = document.querySelectorAll(selector)
    for (var i = 0; i < elements.length; i++) {
        var e = elements[i]
        e.classList.remove(className)
    }
}

/**
 * 删除元素属性
 * */
export const removeAttribute = function (selector, attribute) {
    let elements = document.querySelectorAll(selector)
    for (let i = 0; i < elements.length; i++) {
        let e = elements[i]
        e.removeAttribute(attribute)
    }
}

/**
 * 绑定事件
 * */
export const bindAll = function (selector, eventName, callback) {
    var elements = document.querySelectorAll(selector)
    for (var i = 0; i < elements.length; i++) {
        var e = elements[i]
        bindEvent(e, eventName, callback)
    }
}

// find 函数可以查找 element下的所有子元素
export const findElement = function (element, selector) {
    return element.querySelector(selector)
}

/**
 * 日期格式转换 Wed Dec 18 2019 13:02:40 GMT+0800 (中国标准时间) => yyyy-MM-DD HH:ss:mm
 * */
export const myDate = date => {
    if (date != null) {
        let currentDate = date.toLocaleDateString()
        let hours = date.getHours()
        let min = date.getMinutes()
        let seconds = date.getSeconds()
        let s2 = currentDate.replace(/(\/)/g, '-')
        let dateArr = s2.split('-')
        dateArr[1] = (dateArr[1]) < 10 ? `0${dateArr[1]}` : (dateArr[1])
        dateArr[2] = (dateArr[2]) < 10 ? `0${dateArr[2]}` : (dateArr[2])
        hours = (hours < 10 ? `0${hours}` : parseInt(hours))
        min = (min < 10 ? `0${min}` : parseInt(min))
        seconds = (seconds < 10 ? `0${seconds}` : parseInt(seconds))
        let dated = dateArr.join('-')
        dated = `${dated} ${hours}:${min}:${seconds}`
        return dated
    }
    return null
}

/**
 * 日期格式转换 Wed Dec 18 2019 13:02:40 GMT+0800 (中国标准时间) => yyyy/MM/DD
 * */
export const myDate1 = date => {
    if (date != null) {
        let currentDate = date.toLocaleDateString()
        let s2 = currentDate.replace(/(\/)/g, '-')
        let dateArr = s2.split('-')
        dateArr[1] = (dateArr[1]) < 10 ? `0${dateArr[1]}` : (dateArr[1])
        dateArr[2] = (dateArr[2]) < 10 ? `0${dateArr[2]}` : (dateArr[2])
        let dated = dateArr.join('/')
        return dated
    }
    return null
}

function padLeftZero(str) {
    return ('00' + str).substr(str.length)
}

/**
 * 获取某个日期的所在月份的天数
 * */
export const days = date => {
    if (date != null) {
        let day = 0
        let year = date.getFullYear()
        let month = date.getMonth() + 1
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                day = 30;
                break;
            case 2:
                if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                    day = 29;
                } else {
                    day = 28;
                }
        }
        return day
    }
    return 0
}

/**
 * 去空格
 * */
export const myStr = str => {
    return str.replace(/^\s+|\s+$/gm, '');
}

/**
 * 判断是否是数字
 * */
export const isNumber = number => {
    return number != number
}

// 获取分组默认值
export const getTreeDeepArr = (orgName, treeData) => {
    // 在递归时操作的数组
    let arr = [];
    // 存放结果的数组
    let returnArr = [];
    // 深度
    let depth = 0;
    return childrenEach(treeData, depth);

    // 定义递归函数
    function childrenEach(treeData, depthN) {
        for (let j = 0; j < treeData.length; j++) {
            depth = depthN;
            arr[depthN] = (treeData[j].orgId);
            if (treeData[j].orgName == orgName) {
                returnArr = arr.slice(0, depthN + 1);
                break
            } else {
                if (treeData[j].children) {
                    depth++;
                    childrenEach(treeData[j].children, depth);
                }
            }
        }

        return returnArr;
    }
}

/**
 * 获取当前年月日
 * */
export function getDateYMD() {
    const current = new Date()
    const y = current.getFullYear()
    let MM = current.getMonth() + 1
    let dd = current.getDate()

    MM = MM < 10 ? `0${MM}` : MM
    dd = dd < 10 ? `0${dd}` : dd

    return `${y}${MM}${dd}`
}

/**
 * 锚点
 *
 */
export function jump(domId) {
    // 当前窗口正中心位置到指定dom位置的距离

    //页面滚动了的距离
    let height = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;

    //指定dom到页面顶端的距离
    let dom = document.getElementById(domId);
    let domHeight = dom.offsetTop +27;

    //滚动距离计算
    var S = Number(height) - Number(domHeight);

    //判断上滚还是下滚
    if (S < 0) {
        //下滚
        S = Math.abs(S);
        window.scrollBy({top: S, behavior: "smooth"});
    } else if (S == 0) {
        //不滚
        window.scrollBy({top: 0, behavior: "smooth"});
    } else {
        //上滚
        S = -S
        window.scrollBy({top: S, behavior: "smooth"});
    }
}
